﻿using System;
using System.Collections.Generic;
using System.Reflection;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Utilities.Tests
{
    [TestClass()]
    public class AssemblyModuleTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [Serializable]
        public class AssemblyDataTransfer : MarshalByRefObject
        {
            public string InputValueString { get; set; }

            public int InputValueInt { get; set; }

            public string OutputValueString { get; set; }

            public int OutputValueInt { get; set; }

            public string Name { get; set; }

            public int Count { get; set; }
        }

        [Serializable]
        public class TestProcessor : IAssemblyProcessor
        {
            public AssemblyDataTransfer AssemblyDataTransfer { get; set; }

            public TestProcessor()
            {
                AssemblyDataTransfer = new AssemblyDataTransfer();
            }

            public string LibraryDirectoryPath { get; set; }

            public IList<LogMessage> Process(Assembly assembly)
            {
                IList<LogMessage> messages = new List<LogMessage>();

                AssemblyDataTransfer.Name = assembly.FullName;
                AssemblyDataTransfer.Count = assembly.GetTypes().Length;
                AssemblyDataTransfer.OutputValueString = AssemblyDataTransfer.InputValueString;
                AssemblyDataTransfer.OutputValueInt = AssemblyDataTransfer.InputValueInt;

                // Add some bogus message
                LogMessage message = new LogMessageFactory
                {
                    Key = "Completed",
                    Severity = LogMessageFactory.Severities.Information,
                    Text = "Processing completed"
                }
                .Create();

                messages.Add(message);

                return messages;
            }

        }

        [TestMethod()]
        public void AssemblyModuleLoadFromNotForReflectionAndNoDependenciesTest()
        {
            TestProcessor processor = new TestProcessor();

            processor.AssemblyDataTransfer.InputValueInt = 25;
            processor.AssemblyDataTransfer.InputValueString = "Test string";

            using (AssemblyModule assemblyModule = new AssemblyModule("Test assembly module not for reflection and no dependencies"))
            {
                bool failed;
                IList<LogMessage> messages = assemblyModule.Load(@"..\..\..\..\TestBinaries.Solution\CustomerAddresses\bin\Debug\CustomerAddresses.dll", processor, out failed, forReflectionOnly: false);

                Assert.AreEqual("CustomerAddresses, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", processor.AssemblyDataTransfer.Name);
                Assert.AreEqual(2, processor.AssemblyDataTransfer.Count);
                Assert.AreEqual(25, processor.AssemblyDataTransfer.OutputValueInt);
                Assert.AreEqual("Test string", processor.AssemblyDataTransfer.OutputValueString);

                Assert.AreEqual(1, messages.Count);
                Assert.AreEqual("Processing completed", messages[0].Text);
            }
        }

        [TestMethod()]
        public void AssemblyModuleLoadFromForReflectionAndNoDependenciesTest()
        {
            TestProcessor processor = new TestProcessor();

            processor.AssemblyDataTransfer.InputValueInt = 25;
            processor.AssemblyDataTransfer.InputValueString = "Test string";

            using (AssemblyModule assemblyModule = new AssemblyModule("Test assembly module not for reflection and no dependencies"))
            {
                bool failed;
                IList<LogMessage> messages = assemblyModule.Load(@"..\..\..\..\TestBinaries.Solution\CustomerAddresses\bin\Debug\CustomerAddresses.dll", processor, out failed, forReflectionOnly: true);

                Assert.AreEqual("CustomerAddresses, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", processor.AssemblyDataTransfer.Name);
                Assert.AreEqual(2, processor.AssemblyDataTransfer.Count);
                Assert.AreEqual(25, processor.AssemblyDataTransfer.OutputValueInt);
                Assert.AreEqual("Test string", processor.AssemblyDataTransfer.OutputValueString);

                Assert.AreEqual(1, messages.Count);
                Assert.AreEqual("Processing completed", messages[0].Text);
            }
        }

        [TestMethod()]
        public void AssemblyModuleLoadFromNotForReflectionAndDependenciesTest()
        {
            TestProcessor processor = new TestProcessor();

            processor.AssemblyDataTransfer.InputValueInt = 25;
            processor.AssemblyDataTransfer.InputValueString = "Test string";

            using (AssemblyModule assemblyModule = new AssemblyModule("Test assembly module not for reflection and dependencies"))
            {
                bool failed;
                IList<LogMessage> messages = assemblyModule.Load(@"..\..\..\..\OldDllToTestDependencies\Gonte.ObjectModel.dll", processor, out failed, forReflectionOnly: false);

                Assert.AreEqual("Gonte.ObjectModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", processor.AssemblyDataTransfer.Name);
                Assert.AreEqual(45, processor.AssemblyDataTransfer.Count);
                Assert.AreEqual(25, processor.AssemblyDataTransfer.OutputValueInt);
                Assert.AreEqual("Test string", processor.AssemblyDataTransfer.OutputValueString);

                Assert.AreEqual(1, messages.Count);
                Assert.AreEqual("Processing completed", messages[0].Text);
            }
        }

        [TestMethod()]
        public void AssemblyModuleLoadFromForReflectionAndDependenciesTest()
        {
            TestProcessor processor = new TestProcessor();

            processor.AssemblyDataTransfer.InputValueInt = 25;
            processor.AssemblyDataTransfer.InputValueString = "Test string";

            using (AssemblyModule assemblyModule = new AssemblyModule("Test assembly module not for reflection and dependencies"))
            {
                bool failed;
                IList<LogMessage> messages = assemblyModule.Load(@"..\..\..\..\OldDllToTestDependencies\Gonte.ObjectModel.dll", processor, out failed, forReflectionOnly: true);

                Assert.AreEqual("Gonte.ObjectModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", processor.AssemblyDataTransfer.Name);
                Assert.AreEqual(45, processor.AssemblyDataTransfer.Count);
                Assert.AreEqual(25, processor.AssemblyDataTransfer.OutputValueInt);
                Assert.AreEqual("Test string", processor.AssemblyDataTransfer.OutputValueString);

                Assert.AreEqual(1, messages.Count);
                Assert.AreEqual("Processing completed", messages[0].Text);
            }
        }

        [TestMethod()]
        public void AssemblyModuleLoadFromNotForReflectionAndMissingDependenciesTest()
        {
            TestProcessor processor = new TestProcessor();

            processor.AssemblyDataTransfer.InputValueInt = 25;
            processor.AssemblyDataTransfer.InputValueString = "Test string";

            using (AssemblyModule assemblyModule = new AssemblyModule("Test assembly module not for reflection and dependencies"))
            {
                bool failed;
                IList<LogMessage> messages = assemblyModule.Load(@"..\..\..\..\OldDllToTestMissingDependencies\Gonte.ObjectModel.dll", processor, out failed, forReflectionOnly: false);

                Assert.IsTrue(failed);
                Assert.AreEqual(1, messages.Count);
                Assert.AreEqual("Missing referenced assembly: 'Gonte.Utilities.dll'", messages[0].Text);
            }
        }

        [TestMethod()]
        public void AssemblyModuleLoadFromForReflectionAndMissingDependenciesTest()
        {
            TestProcessor processor = new TestProcessor();

            processor.AssemblyDataTransfer.InputValueInt = 25;
            processor.AssemblyDataTransfer.InputValueString = "Test string";

            using (AssemblyModule assemblyModule = new AssemblyModule("Test assembly module not for reflection and dependencies"))
            {
                bool failed;
                IList<LogMessage> messages = assemblyModule.Load(@"..\..\..\..\OldDllToTestMissingDependencies\Gonte.ObjectModel.dll", processor, out failed, forReflectionOnly: true);

                Assert.IsTrue(failed);
                Assert.AreEqual(1, messages.Count);
                Assert.AreEqual("Missing referenced assembly: 'Gonte.Utilities.dll'", messages[0].Text);
            }
        }
    }
}
